package exp3;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.security.URIParameter;
import java.util.Optional;

@WebServlet(urlPatterns ={"/llongin"})

public class Login_in_exp3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        File file = new File("informaion.txt");
        String name = req.getParameter("username");
        String password = req.getParameter("password");
            try {
                String B;
                StringBuilder result = new StringBuilder();
                BufferedReader br = new BufferedReader(new FileReader(file));
                String s = null;
                while((s = br.readLine())!=null)
                {
                    result.append(s);
                    B=result.toString();
                    System.out.println(B);
                    String BB[]=B.split(" ");
//                    System.out.println(BB[1]);
                    if(BB[0].equals(name)&BB[2].equals(password))
                    {
                        System.out.println("daozheli");
                        userdata user=new userdata(BB[0],BB[1],BB[2],BB[3],BB[4]);
                        HttpSession session;
                        session=req.getSession();
                        session.setAttribute("user",user);
                        System.out.println("afaslgakgsknfgsfklsflslgnkngs");
                        resp.sendRedirect("http://localhost:8080/loginss.jsp");
                    }
                    result.delete(0,result.length());
                }
                br.close();
                resp.sendRedirect("http://localhost:8080/error_login_exp3.jsp");
            } catch (Exception e) {
            }

    }

    public void destroy() {
        super.destroy();
    }
}
